package com.ruoyi.produce.report.productTracing.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.produce.process.planoutsource.domain.ProducePlanOutsource;
import com.ruoyi.produce.process.planoutsource.mapper.ProducePlanOutsourceMapper;
import com.ruoyi.produce.process.receiving.domain.ReceivingReportDto;
import com.ruoyi.produce.process.receiving.mapper.ReceivingReportMapper;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.produce.process.worksheet.mapper.ProduceWorkSheetMapper;
import com.ruoyi.produce.report.productTracing.domain.ProductTracing;
import com.ruoyi.psi.inventory.record.domain.StockInRecord;
import com.ruoyi.psi.inventory.record.mapper.StockInRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 产品追溯
 * 业务层接口
 */
@Service
public class ProductTracingServiceImpl implements IProductTracingService{
    @Autowired
    private ReceivingReportMapper receivingReportMapper; //验收单
    @Autowired
    private ProduceWorkSheetMapper workSheetMapper; //加工单
    @Autowired
    private ProducePlanOutsourceMapper outsourceMapper; //委外
    @Autowired
    private StockInRecordMapper stockInRecordMapper; //入库记录

    /**
     * 通过验收批号查询产品追溯信息
     * @return
     */
    @Override
    public ProductTracing searchProductTracingById(String receivingReportId) {
        ReceivingReportDto receivingReport = receivingReportMapper.selectReceivingReportById(receivingReportId);
        ProductTracing result = new ProductTracing();
        // 来自验收单的信息
        result.setProduceId(receivingReport.getProduceId());
        result.setProduceName(receivingReport.getProduceName());
        result.setBaseCategoryName(receivingReport.getBaseCategoryName());
        result.setBaseUnit(receivingReport.getBaseUnit());
        result.setProduceSpecs(receivingReport.getProduceSpecs());
        result.setProduceBrand(receivingReport.getProduceBrand());
        result.setWarehouseName(receivingReport.getWarehouseName());
        result.setStaffId(receivingReport.getStaffInspectId());
        result.setStaffName(receivingReport.getStaffInspectName());
        result.setSourceType(receivingReport.getSourceType());
        StockInRecord stockInRecord = stockInRecordMapper.selectStockInRecordByAssociationId(receivingReportId);//查找入库记录
        if (!StringUtils.isNotNull(stockInRecord)) {
            ProductTracing empty = new ProductTracing();
            return empty;
        }
        result.setCreateTime(stockInRecord.getCreateTime());
        switch (receivingReport.getSourceType()) {
            case ERPConstant.RECEIVING_WORKSHEET:
                result.setWorkSheetId(receivingReport.getWorkSheetId());
                ProduceWorkSheet produceWorkSheet = workSheetMapper.selectProduceWorkSheetById(receivingReport.getWorkSheetId());
                result.setPlanSelfId(produceWorkSheet.getPlanSelfId());
                break;
            case ERPConstant.RECEIVING_OUTSOURCEING:
                result.setPlanOutsourceId(receivingReport.getWorkSheetId());
                ProducePlanOutsource producePlanOutsource = outsourceMapper.selectProducePlanOutsourceById(receivingReport.getWorkSheetId());
                result.setPlanSelfId(producePlanOutsource.getPlanSelfId());
                break;
            case ERPConstant.RECEIVING_PURCHASE:
                result.setPurchaseOrderId(receivingReport.getWorkSheetId());
        }
        return result;
    }
}
